package main

import (
	mysql "aqielife.cn/spider/db"
	"aqielife.cn/spider/models"
	"aqielife.cn/spider/utils"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"log"
	"strings"
)

var (
	BaseUrl = "http://xinhuanet.com"
)

func parseList(url string) []models.ArticleDetail {
	err, doc := utils.DoRequest(url)
	if err != nil {
		log.Fatal(err)
	}
	var pages []models.ArticleDetail
	// body > div.column_3.column_3b.column_3c.mt20 > div:nth-child(1) > ul
	// body > div.column_3.column_3b.column_3c.mt20 > div.left.w310.center > ul
	doc.Find("div#focusListNews > ul > li").Each(func(i int, s *goquery.Selection) {
		url, _ := s.Find("span > a").Attr("href")
		id := utils.GenID()
		pages = append(pages, models.ArticleDetail{
			ID:  id,
			Url: url,
		})
		parseDetail(url, id)
	})
	return pages
}

func parseDetail(url string, id int64) *models.ArticleDetail {
	var images []string
	err, doc := utils.DoRequest(url)
	if err != nil {
		log.Fatal(err)
	}
	title := doc.Find("div.header.domPC span.title").Text()
	if title == "" {
		title = doc.Find("div.xl-cont-head h1").Text()
	}
	content := doc.Find("div#detail > p").Text()

	doc.Find("div#detail > p").Each(func(i int, s *goquery.Selection) {
		img, _ := s.Find("img").Attr("src")
		if img != "" {
			images = append(images, img)
		}
	})

	detail := models.ArticleDetail{
		ID:      id,
		Url:     url,
		Title:   title,
		Content: utils.DeleteExtraSpace(content),
		Type:    utils.NEWS,
		Images:  strings.Join(images, ","),
	}
	if strings.TrimSpace(detail.Content) == "" || strings.TrimSpace(detail.Title) == "" {
		return nil
	}

	if err := mysql.DB.Create(&detail).Error; err != nil {
		log.Printf("db.Create err : %v", err)
	} else {
		log.Printf("新增成功: %d\n", id)
	}
	return &detail
}

func Start() {
	if err := utils.InitSnowFlake("2021-12-03", 1); err != nil {
		fmt.Println("Init() failed, err = ", err)
		return
	}
	pages := parseList(BaseUrl)
	log.Println("pages", pages)
}

func main() {
	Start()
}
